#tag Window
Begin Window ChoiceWindow
   BackColor       =   16777215
   Backdrop        =   ""
   CloseButton     =   True
   Composite       =   True
   Frame           =   0
   FullScreen      =   False
   HasBackColor    =   False
   Height          =   400
   ImplicitInstance=   True
   LiveResize      =   True
   MacProcID       =   0
   MaxHeight       =   32000
   MaximizeButton  =   False
   MaxWidth        =   32000
   MenuBar         =   ""
   MenuBarVisible  =   True
   MinHeight       =   64
   MinimizeButton  =   True
   MinWidth        =   64
   Placement       =   1
   Resizeable      =   True
   Title           =   "Tago Help"
   Visible         =   True
   Width           =   600
   Begin StaticText StaticText1
      AutoDeactivate  =   True
      Bold            =   True
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   48
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   2
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Tago"
      TextAlign       =   0
      TextColor       =   &h000000
      TextFont        =   "Arial"
      TextSize        =   30
      Top             =   14
      Underline       =   ""
      Visible         =   True
      Width           =   89
   End
   Begin Separator Separator1
      AutoDeactivate  =   True
      Enabled         =   True
      Height          =   7
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   True
      LockTop         =   True
      Scope           =   0
      TabIndex        =   3
      TabPanelIndex   =   0
      TabStop         =   True
      Top             =   67
      Visible         =   True
      Width           =   560
   End
   Begin StaticText Step1_StaticText
      AutoDeactivate  =   True
      Bold            =   True
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   23
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   224
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   True
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   1
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Search the Internet for Movie Names or TV Series"
      TextAlign       =   2
      TextColor       =   &h000000
      TextFont        =   "Arial"
      TextSize        =   14
      Top             =   32
      Underline       =   ""
      Visible         =   True
      Width           =   356
   End
   Begin StaticText ShowOrMovieType_StaticText
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   4
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Show/Movie Type:"
      TextAlign       =   0
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   10
      Top             =   120
      Underline       =   ""
      Visible         =   True
      Width           =   95
   End
   Begin PopupMenu ShowOrMovieType_PopupMenu
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      InitialValue    =   "TV Show\rMovie"
      Italic          =   ""
      Left            =   121
      ListIndex       =   0
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Scope           =   0
      TabIndex        =   5
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   10
      Top             =   121
      Underline       =   ""
      Visible         =   True
      Width           =   126
   End
   Begin StaticText SearchCriteria_StaticText
      AutoDeactivate  =   True
      Bold            =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   ""
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   6
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Search Criteria:"
      TextAlign       =   0
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   10
      Top             =   88
      Underline       =   ""
      Visible         =   True
      Width           =   95
   End
   Begin TextField SearchCriteria_TextField
      AcceptTabs      =   ""
      Alignment       =   0
      AutoDeactivate  =   True
      BackColor       =   &hFFFFFF
      Bold            =   ""
      Border          =   True
      CueText         =   ""
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Format          =   ""
      Height          =   22
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   121
      LimitText       =   0
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Mask            =   ""
      Password        =   ""
      ReadOnly        =   ""
      Scope           =   0
      TabIndex        =   7
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   ""
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   10
      Top             =   86
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   459
   End
   Begin PushButton PushButton1
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "Search"
      Default         =   True
      Enabled         =   True
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   505
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   8
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   10
      Top             =   120
      Underline       =   ""
      Visible         =   True
      Width           =   75
   End
   Begin Separator Separator2
      AutoDeactivate  =   True
      Enabled         =   True
      Height          =   7
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   True
      LockRight       =   True
      LockTop         =   True
      Scope           =   0
      TabIndex        =   9
      TabPanelIndex   =   0
      TabStop         =   True
      Top             =   207
      Visible         =   True
      Width           =   560
   End
   Begin StaticText Step1_StaticText1
      AutoDeactivate  =   True
      Bold            =   True
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Height          =   23
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   314
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   True
      LockTop         =   True
      Multiline       =   ""
      Scope           =   0
      TabIndex        =   10
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   "Search Results"
      TextAlign       =   2
      TextColor       =   &h000000
      TextFont        =   "Arial"
      TextSize        =   14
      Top             =   182
      Underline       =   ""
      Visible         =   True
      Width           =   266
   End
   Begin ListBox SearchResults_ListBox
      AutoDeactivate  =   True
      AutoHideScrollbars=   True
      Bold            =   ""
      Border          =   True
      ColumnCount     =   2
      ColumnsResizable=   ""
      ColumnWidths    =   ""
      DataField       =   ""
      DataSource      =   ""
      DefaultRowHeight=   -1
      Enabled         =   False
      EnableDrag      =   ""
      EnableDragReorder=   ""
      GridLinesHorizontal=   0
      GridLinesVertical=   0
      HasHeading      =   True
      HeadingIndex    =   -1
      Height          =   122
      HelpTag         =   ""
      Hierarchical    =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      InitialValue    =   ""
      Italic          =   ""
      Left            =   20
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      RequiresSelection=   ""
      Scope           =   0
      ScrollbarHorizontal=   ""
      ScrollBarVertical=   True
      SelectionType   =   0
      TabIndex        =   11
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   0
      Top             =   226
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   253
      _ScrollWidth    =   -1
   End
   Begin TextArea SearchResults_TextArea
      AcceptTabs      =   ""
      Alignment       =   0
      AutoDeactivate  =   True
      BackColor       =   &hFFFFFF
      Bold            =   ""
      Border          =   True
      DataField       =   ""
      DataSource      =   ""
      Enabled         =   True
      Format          =   ""
      Height          =   122
      HelpTag         =   ""
      HideSelection   =   True
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   285
      LimitText       =   0
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Mask            =   ""
      Multiline       =   True
      ReadOnly        =   ""
      Scope           =   0
      ScrollbarHorizontal=   ""
      ScrollbarVertical=   True
      Styled          =   True
      TabIndex        =   12
      TabPanelIndex   =   0
      TabStop         =   True
      Text            =   ""
      TextColor       =   &h000000
      TextFont        =   "System"
      TextSize        =   10
      Top             =   226
      Underline       =   ""
      UseFocusRing    =   True
      Visible         =   True
      Width           =   295
   End
   Begin PushButton AcceptThisTitle_PushButton
      AutoDeactivate  =   True
      Bold            =   ""
      Cancel          =   ""
      Caption         =   "Use this Title"
      Default         =   ""
      Enabled         =   False
      Height          =   20
      HelpTag         =   ""
      Index           =   -2147483648
      InitialParent   =   ""
      Italic          =   ""
      Left            =   458
      LockBottom      =   ""
      LockedInPosition=   False
      LockLeft        =   ""
      LockRight       =   ""
      LockTop         =   ""
      Scope           =   0
      TabIndex        =   14
      TabPanelIndex   =   0
      TabStop         =   True
      TextFont        =   "System"
      TextSize        =   10
      Top             =   360
      Underline       =   ""
      Visible         =   True
      Width           =   122
   End
End
#tag EndWindow

#tag WindowCode
	#tag Event
		Sub Open()
		  _dict_imdb_overview = new dictionary
		  self.SearchCriteria_TextField.SetFocus
		  
		End Sub
	#tag EndEvent


	#tag Note, Name = Untitled
	#tag EndNote


#tag EndWindowCode

#tag Events PushButton1
	#tag Event
		Sub Action()
		  self.ShowOrMovieType_StaticText.Enabled = false
		  self.ShowOrMovieType_PopupMenu.Enabled = false
		  self.SearchCriteria_StaticText.Enabled = false
		  self.SearchCriteria_TextField.Enabled = false
		  me.Enabled = false
		  self.SearchResults_ListBox.DeleteAllRows
		  self.SearchResults_TextArea.text = ""
		  self.Step1_StaticText1.text = "Search Results"
		  
		  if ChoiceWindow.ShowOrMovieType_PopupMenu.text = "TV Show" then
		    dim valid as integer = 0
		    self.SearchResults_ListBox.DeleteAllRows
		    dim s as string = thetvdb_search + EncodeURLComponent(self.SearchCriteria_TextField.text)
		    // MsgBox(s)
		    dim xml_dict as new dictionary
		    xml_dict = http_get( s ,30,1)
		    dim xmlstring as string = xml_dict.Value("http_content")
		    // MsgBox(xmlstring)
		    if instr(xmlstring,"Data") <> 0 then
		      
		      Dim myRegEx_TV As new RegEx
		      Dim myMatch_TV As new RegExMatch
		      myRegEx_TV.Options.DotMatchAll = true
		      myRegEx_TV.SearchPattern="(<Series>.*?</Series>)"
		      myMatch_TV=myRegEx_TV.search(xmlstring)
		      
		      if  myMatch_TV <> Nil then
		        dim res as XmlNodeList
		        while myMatch_TV <> nil
		          if instr(myMatch_TV.SubExpressionString(1),"<id></id>")  = 0 then
		            if instr(myMatch_TV.SubExpressionString(1),"<Overview></Overview>")  = 0 then
		              if instr(myMatch_TV.SubExpressionString(1),"<Overview>")  <> 0 then
		                Dim XMLDoc as XmlDocument = new XmlDocument
		                Try
		                  XmlDoc.PreserveWhitespace = False
		                  XMLDoc.LoadXml( myMatch_TV.SubExpressionString(1))
		                  // MsgBox(myMatch_TV.SubExpressionString(1))
		                  
		                  dim seriesid as string
		                  res  = XmlDoc.Xql("//Series/seriesid" )
		                  if res <> nil then
		                    // MsgBox("//Series/seriesid: " + res.Item(0).ToString)
		                    seriesid = res.Item(0).Child(0).value
		                  else
		                    MsgBox(str(res.LastError))
		                  end if
		                  
		                  dim SeriesName as string
		                  res  = XmlDoc.Xql("//Series/SeriesName" )
		                  if res <> nil then
		                    // MsgBox("//Series/SeriesName: " + res.Item(0).ToString)
		                    SeriesName = res.Item(0).Child(0).value
		                  end if
		                  
		                  dim Overview as string
		                  res  = XmlDoc.Xql("//Series/Overview" )
		                  if res <> nil then
		                    //MsgBox("//Series/Overview: " + res.Item(0).ToString)
		                    Overview = res.Item(0).Child(0).value
		                  end if
		                  
		                  if seriesid.len > 0 then
		                    valid = 1
		                    self.SearchResults_ListBox.AddRow seriesid
		                    self.SearchResults_ListBox.cell(self.SearchResults_ListBox.ListCount - 1,0) = seriesid
		                    self.SearchResults_ListBox.cell(self.SearchResults_ListBox.ListCount - 1,1) = ReplaceAll(SeriesName,"&","&&")
		                    _dict_imdb_overview.value(seriesid)  = "TVDB: " + seriesid + endofline + "Name: " + ReplaceAll(SeriesName,"&","&&") + endofline + ReplaceAll(Overview,"&","&&")
		                  end if
		                Catch
		                  MsgBox "Invalid XML file returned from IMDB."
		                End Try
		              end if
		            end if
		          end if
		          myMatch_TV=myRegEx_TV.search()
		        wend
		        self.SearchResults_ListBox.Enabled = true
		        
		      end if
		      if valid = 0 then
		        self.Step1_StaticText1.text = "No Results Found!"
		        
		        self.SearchResults_ListBox.Enabled = false
		        self.SearchResults_TextArea.Enabled = false
		        self.AcceptThisTitle_PushButton.Enabled = false
		      else
		        self.Step1_StaticText1.text = "Results Found!"
		      end if
		    else
		      MsgBox("Could not fetch movie information for: " + self.SearchCriteria_TextField.text + ".")
		    end if
		  else
		    dim valid as integer = 0
		    self.SearchResults_ListBox.DeleteAllRows
		    dim s as string = themoviedb_lookupsearch_uri +  themoviedb_apikey + "/" + EncodeURLComponent(self.SearchCriteria_TextField.text)
		    // MsgBox(s)
		    dim xml_dict as new dictionary
		    xml_dict = http_get( s ,30,1)
		    dim xmlstring as string = xml_dict.Value("http_content")
		    // MsgBox(xmlstring)
		    if instr(xmlstring,"OpenSearchDescription") <> 0 then
		      
		      Dim myRegEx_MOVIE As new RegEx
		      Dim myMatch_MOVIE As new RegExMatch
		      myRegEx_MOVIE.Options.DotMatchAll = true
		      myRegEx_MOVIE.SearchPattern="(<movie>.*?</movie>)"
		      myMatch_MOVIE=myRegEx_MOVIE.search(xmlstring)
		      
		      if  myMatch_MOVIE <> Nil then
		        dim res as XmlNodeList
		        while myMatch_MOVIE <> nil
		          if instr(myMatch_MOVIE.SubExpressionString(1),"<imdb_id></imdb_id>")  = 0 then
		            if instr(myMatch_MOVIE.SubExpressionString(1),"<overview></overview>")  = 0 then
		              Dim XMLDoc as XmlDocument = new XmlDocument
		              Try
		                XmlDoc.PreserveWhitespace = False
		                XMLDoc.LoadXml( myMatch_MOVIE.SubExpressionString(1))
		                
		                dim imdb_id as string
		                if res <> nil then
		                  res  = XmlDoc.Xql("//movie/imdb_id" )
		                  // MsgBox("//movie/imdb_id: " + res.Item(0).ToString)
		                  imdb_id = res.Item(0).Child(0).value
		                end if
		                
		                dim name as string
		                res  = XmlDoc.Xql("//movie/name" )
		                if res <> nil then
		                  // MsgBox("//movie/name: " + res.Item(0).ToString)
		                  name = res.Item(0).Child(0).value
		                end if
		                
		                dim overview as string
		                res  = XmlDoc.Xql("//movie/overview" )
		                if res <> nil then
		                  //MsgBox("//movie/overview: " + res.Item(0).ToString)
		                  overview = res.Item(0).Child(0).value
		                end if
		                
		                if instr(imdb_id,"tt") <> 0 then
		                  valid = 1
		                  self.SearchResults_ListBox.AddRow imdb_id
		                  self.SearchResults_ListBox.cell(self.SearchResults_ListBox.ListCount - 1,0) = imdb_id
		                  self.SearchResults_ListBox.cell(self.SearchResults_ListBox.ListCount - 1,1) = ReplaceAll(name,"&","&&")
		                  
		                  _dict_imdb_overview.value(imdb_id)  = "IMDB# " + imdb_id + endofline + "Name: " + ReplaceAll(name,"&","&&") + endofline + ReplaceAll(overview,"&","&&")
		                end if
		              Catch
		                MsgBox "Invalid XML file returned from IMDB."
		              End Try
		            end if
		          end if
		          myMatch_MOVIE=myRegEx_MOVIE.search()
		        wend
		        self.SearchResults_ListBox.Enabled = true
		        
		      end if
		      if valid = 0 then
		        self.Step1_StaticText1.text = "No Results Found!"
		        
		        self.SearchResults_ListBox.Enabled = false
		        self.SearchResults_TextArea.Enabled = false
		        self.AcceptThisTitle_PushButton.Enabled = false
		        
		      else
		        self.Step1_StaticText1.text = "Results Found!"
		      end if
		    else
		      MsgBox("Could not fetch TV Show information for: " + self.SearchCriteria_TextField.text + ".")
		    end if
		  end if
		  
		  me.Enabled = true
		  self.SearchCriteria_TextField.Enabled = true
		  self.SearchCriteria_StaticText.Enabled = true
		  self.ShowOrMovieType_PopupMenu.Enabled = true
		  self.ShowOrMovieType_StaticText.Enabled = true
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events SearchResults_ListBox
	#tag Event
		Sub Change()
		  if _dict_imdb_overview.haskey(me.text) then
		    self.SearchResults_TextArea.text = _dict_imdb_overview.value(me.text)
		    if self.AcceptThisTitle_PushButton.Enabled <> true then self.AcceptThisTitle_PushButton.Enabled = true
		  end if
		End Sub
	#tag EndEvent
	#tag Event
		Sub Open()
		  me.Heading(0) = "Series ID"
		  me.Heading(1) = "Series Name"
		  
		  me.ColumnWidths = "30%,70%"
		End Sub
	#tag EndEvent
#tag EndEvents
#tag Events AcceptThisTitle_PushButton
	#tag Event
		Sub Action()
		  dim upperbound as integer = self.SearchResults_ListBox.ListCount-1
		  dim j as integer
		  for j=0 to upperbound
		    if self.SearchResults_ListBox.cell(j,0) = self.SearchResults_ListBox.text then
		      _dict_exact_series_name_to_seriesid.value(self.SearchResults_ListBox.cell(j,1)) = self.SearchResults_ListBox.cell(j,0)
		      if MainWindow.ShowOrMovieName_TextField.enabled = true then MainWindow.ShowOrMovieName_TextField.text = self.SearchResults_ListBox.cell(j,1)
		      if MainWindow.desc_title_TextField.enabled = true then MainWindow.desc_title_TextField.text = self.SearchResults_ListBox.cell(j,1)
		      exit
		    end if
		  next
		  
		  
		  if ChoiceWindow.ShowOrMovieType_PopupMenu.text = "TV Show" then
		    MainWindow.TVSeriesId_TextField.text = self.SearchResults_ListBox.text
		    MainWindow.ShowOrMovieType_PopupMenu.ListIndex = 0
		  else
		    MainWindow.IMDB_TextField.text = right(self.SearchResults_ListBox.text,self.SearchResults_ListBox.text.len - 2)
		    MainWindow.ShowOrMovieType_PopupMenu.ListIndex = 3
		  end if
		  me.enabled = false
		  self.SearchResults_ListBox.DeleteAllRows
		  self.SearchResults_ListBox.visible = true
		  self.SearchResults_TextArea.text = ""
		  self.SearchResults_TextArea.visible = true
		  self.Step1_StaticText1.text = "Search Results"
		  self.close
		  
		End Sub
	#tag EndEvent
#tag EndEvents
